# Created by 黄景涛
# DATE: 2025/3/4
import os.path

from config.settings import ROOT_PATH
import yaml
import ruamel


class Yaml:
    """ yaml文件操作 """

    def __init__(self, relative_file_path):
        self.file_path = os.path.join(ROOT_PATH, relative_file_path)

    def read(self) -> dict:
        """ 加载文件内容 """
        with open(self.file_path, mode='r', encoding='utf8') as f:
            content = yaml.safe_load(f)
        return content

    def update(self, key, val):
        """ 更新内容 """
        r_yaml = ruamel.yaml.YAML()
        r_yaml.preserve_quotes = True
        r_yaml.indent(mapping=2, sequence=4, offset=2)

        with open(self.file_path, 'r', encoding='utf-8') as f:
            content = r_yaml.load(f)

        with open(self.file_path, 'w', encoding='utf-8') as f:
            if key in content:
                content[key] = val
            else:
                raise Exception(f'key不存在：{key}')
            r_yaml.dump(content, f)
        
    def del_key(self):
        """ 删除指定key """


if __name__ == '__main__':
    ym = Yaml('config/config.yaml')
    print(ym.read())
